import {Navigate, Outlet, Route, Routes} from "react-router-dom";
import {useUserToken} from "@/store/userStore.ts";
import {ProtectedRoute} from "@/components/ProtectedRoute.tsx";
import {ROUTE_PATHS} from "@/routs/routes.ts";
import {LoginPage} from "@/pages/LoginPage.tsx";
import {TicketPage} from "@/pages/TicketPage";
import {ProfilePage} from "@/pages/ProfilePage.tsx";
import {QRScannerPage} from "@/pages/QRScannerPage.tsx";
import {EditPwdPage} from "@/pages/EditPwdPage.tsx";


export function AppRoutes() {
    const {token} = useUserToken();

    // 根据认证状态确定默认重定向路径
    const defaultRedirectPath = token ? ROUTE_PATHS.TICKET : ROUTE_PATHS.LOGIN;

    return (
        <Routes>
            {/* 登录路由 */}
            <Route
                path={ROUTE_PATHS.LOGIN}
                element={
                    token ? (
                        <Navigate to={ROUTE_PATHS.TICKET} replace/>
                    ) : (
                        <LoginPage/>
                    )
                }
            />

            {/* 受保护的路由组 */}
            <Route element={<ProtectedRoute><Outlet/></ProtectedRoute>}>
                <Route path={ROUTE_PATHS.TICKET} element={<TicketPage/>}/>
                <Route path={ROUTE_PATHS.PROFILE} element={<ProfilePage/>}/>
                <Route path={ROUTE_PATHS.QR_SCANNER} element={<QRScannerPage/>}/>
                <Route path={ROUTE_PATHS.EDIT_PWD} element={<EditPwdPage/>}/>
            </Route>

            {/* 根路由和404路由 */}
            <Route path={ROUTE_PATHS.ROOT} element={<Navigate to={defaultRedirectPath} replace/>}/>
            <Route path="*" element={<Navigate to={defaultRedirectPath} replace/>}/>
        </Routes>
    );
}